"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var api_1 = require("../../../../config/api");
var banner_service_1 = require("../banner.service");
var category_service_1 = require("../../category/category.service");
var AddComponent = /** @class */ (function () {
    function AddComponent(adOssUploadService, _message, bannerService, categoryService, router, route, el) {
        this.adOssUploadService = adOssUploadService;
        this._message = _message;
        this.bannerService = bannerService;
        this.categoryService = categoryService;
        this.router = router;
        this.route = route;
        this.el = el;
        this.addActivity = new AddActivity();
        this.editActivity = new EditActivity();
        this.fileList = [];
        this.picList = [];
        this.task_ids = [];
        this.checkedTaskInfo = [];
        this.taskModalVisible = false;
        this.taskPage = 1;
        this.taskPageSize = 5;
        this.taskList = [];
        this.taskTotal = 0;
        this.submitStatus = false;
        this.isShow = false;
        this.editId = null;
        this.isChange = false;
        this.canChange = false;
        this.categoryList = [];
        this.merchant_id = '';
        this.activity_type = '';
        this.first_category = '';
        this.second_category = '';
        this.task_name = '';
        this.selectedGroup = null;
        this.typeList = [];
        this.isConfirmTaskLoading = false;
        this.picSizeInfo = '图片尺寸建议上传 701px * 351px';
        this.picError = false;
        this.nameError = false;
        this.commentError = false;
        this.nameRequired = false;
        this.commentRequired = false;
        this.sortRequired = false;
        this.taskRequired = false;
        this.sortError = false;
    }
    AddComponent.prototype.ngOnInit = function () {
        var id = this.route.snapshot.queryParams['id'];
        if (id) {
            this.getActivityInfo(id);
            this.editId = id;
        }
        this.getCategoryList();
    };
    AddComponent.prototype.fileChange = function () {
        if (this.canChange) {
            this.isChange = true;
        }
        if (this.fileList.length == 0 && this.picList.length == 0) {
            this.picError = true;
        }
        else {
            this.picError = false;
        }
    };
    AddComponent.prototype.WidthCheck = function (type, str, maxLen, e) {
        var w = 0;
        var tempCount = 0;
        if (type == 'name') {
            if (e.length == 0) {
                this.nameRequired = true;
            }
            else {
                this.nameRequired = false;
            }
        }
        else {
            if (e.length == 0) {
                this.commentRequired = true;
            }
            else {
                this.commentRequired = false;
            }
        }
        for (var i = 0; i < str.length; i++) {
            var c = str.charCodeAt(i);
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                w++;
            }
            else {
                w += 2;
            }
            if (w > maxLen) {
                // str = str.substr(0, i);
                if (type == 'name') {
                    // this.el.nativeElement.querySelector('#activity_name').value = str;
                    this.nameError = true;
                }
                else {
                    // this.el.nativeElement.querySelector('#comment').value = str;
                    this.commentError = true;
                }
            }
            else {
                if (type == 'name') {
                    this.nameError = false;
                }
                else {
                    this.commentError = false;
                }
            }
        }
    };
    AddComponent.prototype.search = function () {
        this.taskPage = 1;
        this.getTaskList();
    };
    AddComponent.prototype.reset = function () {
        this.merchant_id = '';
        this.activity_type = '';
        this.first_category = '';
        this.second_category = '';
        this.task_name = '';
        this.taskPage = 1;
        this.getTaskList();
    };
    AddComponent.prototype.getActivityInfo = function (id) {
        var _this = this;
        this.bannerService.getActivityInfo(id).subscribe(function (res) {
            if (res.code == 200) {
                var data = res.data;
                _this.addActivity.activity_name = data.activity_name;
                _this.addActivity.type = data.type;
                _this.addActivity.sort = data.sort;
                _this.addActivity.comment = data.comment;
                var task_ids_1 = JSON.parse(data.task_ids);
                _this.checkedTaskInfo = [];
                _this.bannerService.getTaskList(1, 666).subscribe(function (res) {
                    if (res.code == 200) {
                        var taskList = res.data.list.data;
                        taskList.map(function (e, i) {
                            e['taskPreview'] = api_1.AliyunOssOutputUrl + "/" + e['task_content']['image'] + "?update=" + new Date().getTime();
                            task_ids_1.map(function (e1, i1) {
                                if (e.id == e1) {
                                    _this.task_ids.push(e);
                                }
                            });
                        });
                        _this.checkedTaskInfo = _this.task_ids;
                    }
                });
                if (data.pic) {
                    _this.picList.push({ src: api_1.AliyunOssOutputUrl + "/" + data.pic + "?update=" + new Date().getTime() });
                    _this.fileList.push('pic');
                    _this.canChange = true;
                }
            }
        });
    };
    AddComponent.prototype.getTaskList = function () {
        var _this = this;
        this.bannerService.getTaskList(this.taskPage, this.taskPageSize).subscribe(function (res) {
            if (res.code === 200) {
                var data = res.data;
                _this.taskList = data.list.data;
                _this.taskTotal = data.list.total;
                var task_type = data.task_type;
                for (var key in task_type) {
                    _this.typeList.push({
                        'label': task_type[key],
                        'value': key
                    });
                }
                ;
                _this.taskList.map(function (e, i) {
                    e['taskPreview'] = api_1.AliyunOssOutputUrl + "/" + e['task_content']['image'] + "?update=" + new Date().getTime();
                    e['checked'] = false;
                    if (_this.checkedTaskInfo.length > 0) {
                        _this.checkedTaskInfo.map(function (e1, i1) {
                            if (e.id == e1.id) {
                                e['checked'] = true;
                            }
                        });
                    }
                });
            }
        });
    };
    AddComponent.prototype.getCategoryList = function () {
        var _this = this;
        this.categoryService.getCategoryList(1, 0).subscribe(function (res) {
            if (res.code === 200) {
                _this.categoryList = res.data;
                console.log(_this.categoryList);
                _this.addActivity.category_id = _this.categoryList[0]['id'];
            }
        });
    };
    AddComponent.prototype.closeTaskModal = function () {
        this.taskModalVisible = false;
    };
    AddComponent.prototype.addTask = function (e) {
        e.stopPropagation();
        this.taskModalVisible = true;
        this.getTaskList();
    };
    AddComponent.prototype.dealTask = function (e, data) {
        var _this = this;
        this.taskList.map(function (item, i) {
            if (item.id == data.id) {
                if (_this.task_ids.length == 0) {
                    if (e) {
                        item['checked'] = true;
                        _this.task_ids.push(data);
                    }
                }
                else {
                    if (e) {
                        var num_1 = 0;
                        _this.task_ids.map(function (item1, i) {
                            if (item1.id != data.id && num_1 === 0) {
                                item['checked'] = true;
                                _this.task_ids.push(data);
                                num_1++;
                            }
                            return;
                        });
                    }
                    else {
                        _this.task_ids.map(function (item2, i) {
                            if (item2.id == data.id) {
                                item['checked'] = false;
                                _this.task_ids.splice(i, 1);
                                return;
                            }
                            return;
                        });
                    }
                }
            }
        });
        this.checkedTaskInfo = this.task_ids;
        if (this.checkedTaskInfo.length > 0) {
            this.taskRequired = false;
        }
        else {
            this.taskRequired = true;
        }
    };
    AddComponent.prototype.uploadImg = function (activity_id) {
        var _this = this;
        this.adOssUploadService
            .getSignature('/v1/admin/tuike_activity/' + activity_id + '?callback=true')
            .subscribe(function (res1) {
            var length = _this.fileList.filter(function (item) {
                return item && item != "pic";
            }).length;
            var successLen = 0;
            if (length == 0) {
                _this.submitStatus = false;
                if (_this.editId) {
                    _this._message.create('success', '修改成功');
                }
                else {
                    _this._message.create('success', '添加成功');
                }
                _this.router.navigate(['tuike/activity/list']);
                return false;
            }
            _this.fileList.every(function (val, idx, array) {
                if (val && val != "pic") {
                    _this.adOssUploadService.fileUpload(val, res1.data, 'tuike_activity_pic' + '.' + val.name.replace(/.+\./, '').toLowerCase(), 'image')
                        .subscribe(function (res2) {
                        successLen++;
                        if (successLen == length) {
                            _this.submitStatus = false;
                            if (_this.editId) {
                                _this._message.create('success', '修改成功');
                            }
                            else {
                                _this._message.create('success', '添加成功');
                            }
                            _this.router.navigate(['tuike/activity/list']);
                        }
                    });
                }
                return true;
            });
        });
    };
    AddComponent.prototype.goBack = function () {
        this.router.navigate(['tuike/activity/list']);
    };
    AddComponent.prototype.addActivityFormSubmit = function (e) {
        var _this = this;
        e.stopPropagation();
        var flag = true;
        if (this.checkedTaskInfo.length == 0) {
            this.taskRequired = true;
        }
        else {
            this.taskRequired = false;
            flag = true;
        }
        if (this.addActivity.activity_name === '') {
            this.nameRequired = true;
        }
        else {
            this.nameRequired = false;
            flag = true;
        }
        if (this.addActivity.sort === '') {
            this.sortRequired = true;
        }
        else {
            this.sortRequired = false;
            flag = true;
        }
        if (this.addActivity.comment === '') {
            this.commentRequired = true;
        }
        else {
            this.commentRequired = false;
            flag = true;
        }
        this.fileChange();
        if (this.checkedTaskInfo.length == 0 || this.addActivity.activity_name === '' || this.addActivity.sort === '' || this.picError || this.addActivity.comment === '' || this.nameError || this.commentError || this.sortError) {
            flag = false;
        }
        var data = Object.assign(this.addActivity);
        data['task_ids'] = [];
        this.checkedTaskInfo.map(function (e, i) {
            data['task_ids'].push(e.id);
        });
        if (flag) {
            this.submitStatus = true;
            this.bannerService.addActivity(data).subscribe(function (res) {
                if (res.code == 200) {
                    var id = res.data.id;
                    _this.uploadImg(id);
                }
            });
        }
    };
    AddComponent.prototype.checkSort = function (e) {
        console.log(e);
        if (e < 0) {
            this.sortError = true;
        }
        else {
            this.sortError = false;
        }
        if (!/^[0-9]+$/.test(e + '') && e !== '') {
            this.sortError = true;
        }
        else {
            this.sortError = false;
        }
        if (e === '') {
            this.sortRequired = true;
        }
        else {
            this.sortRequired = false;
        }
    };
    AddComponent.prototype.editActivityFormSubmit = function (e) {
        var _this = this;
        e.stopPropagation();
        var flag = true;
        if (this.checkedTaskInfo.length == 0) {
            this.taskRequired = true;
        }
        else {
            this.taskRequired = false;
            flag = true;
        }
        if (this.addActivity.activity_name === '') {
            this.nameRequired = true;
        }
        else {
            this.nameRequired = false;
            flag = true;
        }
        if (this.addActivity.sort === '') {
            this.sortRequired = true;
        }
        else {
            this.sortRequired = false;
            flag = true;
        }
        if (this.addActivity.comment === '') {
            this.commentRequired = true;
        }
        else {
            this.commentRequired = false;
            flag = true;
        }
        this.fileChange();
        if (this.checkedTaskInfo.length == 0 || this.addActivity.activity_name === '' || this.addActivity.sort === '' || this.picError || this.addActivity.comment === '' || this.nameError || this.commentError || this.sortError) {
            flag = false;
        }
        var data = Object.assign(this.addActivity);
        data['id'] = this.editId;
        data['task_ids'] = [];
        this.checkedTaskInfo.map(function (e, i) {
            data['task_ids'].push(e.id);
        });
        if (flag) {
            this.submitStatus = true;
            this.bannerService.editActivity(data).subscribe(function (res) {
                if (res.code == 200) {
                    _this.uploadImg(_this.editId);
                    // if (this.isChange) {
                    //   this.uploadImg(this.editId);
                    // } else {
                    //   this.uploadImg(this.editId);
                    //   this.submitStatus = false;
                    //   this._message.create('success', '修改成功');
                    //   this.router.navigate(['tuike/activity']);
                    // }
                }
            });
        }
    };
    AddComponent = __decorate([
        core_1.Component({
            selector: 'app-add',
            templateUrl: './add.component.html',
            styleUrls: ['./add.component.scss'],
            providers: [banner_service_1.BannerService, category_service_1.CategoryService]
        })
    ], AddComponent);
    return AddComponent;
}());
exports.AddComponent = AddComponent;
var AddActivity = /** @class */ (function () {
    function AddActivity() {
        this.activity_name = '';
        this.type = '2';
        this.comment = '';
        this.sort = '';
        this.category_id = '';
    }
    return AddActivity;
}());
var EditActivity = /** @class */ (function () {
    function EditActivity() {
        this.activity_name = '';
        this.type = '';
        this.comment = '';
        this.sort = '';
        this.category_id = '';
    }
    return EditActivity;
}());
